<template>
  <Category :scence="scence"></Category>
  <el-card style="margin: 10px 0px">
    <div v-show="scence == 0">
      <el-button
        type="primary"
        icon="Plus"
        :disabled="categoryStore.C3Id ? false : true"
        @click="addSpu"
      >
        添加SPU
      </el-button>
      <el-table style="margin: 10px 0px" border :data="spuData">
        <el-table-column label="序号" type="index" width="80" align="center" />
        <el-table-column label="SPU名称" prop="spuName" />
        <el-table-column
          label="SPU描述"
          prop="description"
          show-overflow-tooltip="true"
        />
        <el-table-column label="SPU操作">
          <template #="{ row, $index }">
            <el-button type="primary" size="small" icon="plus"></el-button>
            <el-button
              type="warning"
              size="small"
              icon="edit"
              @click="editSpu(row)"
            ></el-button>
            <el-button
              type="info"
              size="small"
              icon="view"
              @click="showSkuInfo(row)"
            ></el-button>

            <el-popconfirm
              :title="`确定要删除${row.spuName}吗?`"
              width="200px"
              @confirm="deleteSpu(row.id)"
            >
              <template #reference>
                <el-button type="danger" size="small" icon="delete"></el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!--分页器组件-->
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[3, 5, 7, 9]"
        :background="true"
        layout="prev, pager, next, jumper, ->, sizes, total"
        :total="pageCount"
        @size-change="handleSizeChange"
        @current-change="getSpuList"
      />
    </div>
    <SpuForm
      ref="spuFromInstance"
      v-show="scence == 1"
      @changeScence="changeScence"
    ></SpuForm>
    <el-dialog v-model="dialogShow" title="SKU列表">
      <el-table :data="skuResponseData" border style="width: 100%">
        <el-table-column prop="skuName" label="Sku名称" />
        <el-table-column prop="price" label="Sku价格" width="100" />
        <el-table-column prop="weight" label="Sku重量" width="100" />
        <el-table-column label="Sku图片" width="150">
          <template #="{ row, $index }">
            <img :src="row.skuDefaultImg" style="width: 100px; height: 100px" />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </el-card>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue'
import { ElMessage } from 'element-plus'
//引入请求方法
import { GetSpuPageList, DeleteSpu } from '@/api/product/spu'
import { GetSkuInfoBySpuID } from '@/api/product/sku'
//引入类型
import type {
  SpuResponseData,
  SpuRecords,
  SpuRecord,
} from '@/api/product/spu/type'
import type { SkuRecord } from '@/api/product/sku/type'
//引入子组件
import SpuForm from './spuForm.vue'
//引入category仓库
import useCategoryStore from '@/store/modules/category'

let categoryStore = useCategoryStore()
//场景切换控制参数
let scence = ref<number>(0)
//当前页
let currentPage = ref<number>(1)
//每页显示条数
let pageSize = ref<number>(3)
//总页数
let pageCount = ref<number>(0)
//接收SpuForm对象
let spuFromInstance = ref()

//对仓库中类别3Id值监视
watch(
  () => categoryStore.C3Id,
  () => {
    //如果类别3ID有值 ，则发送请求
    if (categoryStore.C3Id) {
      getSpuList()
    }
  },
)
//存储已有的spu数据
let spuData = ref<SpuRecords>([])
//发送请求方法
const getSpuList = async (pager: number = 1) => {
  currentPage.value = pager
  let result: SpuResponseData = await GetSpuPageList(
    currentPage.value,
    pageSize.value,
    categoryStore.C3Id,
  )
  if (result.code == 200) {
    spuData.value = result.data.records
    pageCount.value = result.data.total
  }
}
//当分页器页数发生改变时回调
const handleSizeChange = (value: number) => {
  pageSize.value = value
  //重新加载数据
  getSpuList()
}
//添加spu按钮回调
const addSpu = () => {
  scence.value = 1
}

//修改spu按钮回调
const editSpu = (row: SpuRecord) => {
  scence.value = 1
  //调用子组件中的方法
  spuFromInstance.value.initSpuData(row)
}

//修改场景回调,通过自定义事件，子组件调用父组件的方法
const changeScence = (value: number) => {
  scence.value = value
  //重新添加已有的Spu
  getSpuList()
}

//定义弹出框架是否显示
let dialogShow = ref<boolean>(false)
//定义sku数据
let skuResponseData = ref<SkuRecord[]>([])
//获取spu下的sku数据
const showSkuInfo = async (row: SpuRecord) => {
  const result = await GetSkuInfoBySpuID(row.id)
  if (result.code == 200) {
    dialogShow.value = true
    skuResponseData.value = result.data
  }
}
//删除指定Spu
const deleteSpu = async (id: number) => {
  const result = await DeleteSpu(id)
  if (result.code == 200) {
    ElMessage.success('删除成功!')
    getSpuList(currentPage.value)
  } else {
    ElMessage.error(result.message)
  }
}

//组件消毁清除类型信息
onBeforeUnmount(() => {
  categoryStore.$reset()
})
</script>

<style scoped></style>
